<?php

require FCPATH.'branch/fqb/D_Admin_Form.php';

class Form_wenjuan extends D_Admin_Form {

	public function __construct() {
		parent::__construct();
	}


    public function index() {

        if (!$this->cid) {
            $this->admin_msg('请在内容管理的右侧链接进入查看问卷调查');
        }

        $cdata = $this->db->where('id', $this->cid)->get(SITE_ID.'_'.APP_DIR.'_data_'.intval($this->cdata['tableid']))->row_array();
        if (!$cdata) {
            $this->admin_msg('问卷调查不存在');
        }


        $table = SITE_ID.'_'.APP_DIR.'_form_'.$this->fid;
        $data = $this->db->where('cid', $this->cid)->get($table)->result_array();
        if (!$data) {
            $this->admin_msg('没有任何调查数据');
        }

        $value = array();
        $wenjuan = dr_string2array($cdata['wjnr']);
        if (!$wenjuan) {
            $this->admin_msg('没有任何调查选项');
        }

        // 初始化
        foreach ($wenjuan as $i => $t) {
            if ($t['lx'] == 3) {
                // 文本类型
                $value[$i] = array();
            } else {
                // 单选
                $xx = explode(PHP_EOL, $t['xx']);
                if ($xx) {
                    foreach ($xx as $n) {
                        $value[$i][$n] = 0;
                    }
                }
            }
        }


        foreach ($data as $t) {
            $wj = dr_string2array($t['content']);
            if ($wj) {
                foreach ($wj as $i => $w) {
                    if ($wenjuan[$i] && $w) {
                        !isset($value[$i]) && $value[$i] = array();
                        if ($wenjuan[$i]['lx'] == 3) {
                            // 文本类型
                            $value[$i][] = (string)$w;
                        } else if ($wenjuan[$i]['lx'] == 2) {
                            // 多选
                            $w = dr_string2array($w);
                            if ($w && is_array($w)) {
                                foreach ($w as $n) {
                                    $value[$i][$n] = (int)$value[$i][$n] + 1;
                                }
                            }
                        } else {
                            // 单选
                            if (!is_array($w)) {
                                $value[$i][$w] = isset($value[$i][$w]) ? (int)$value[$i][$w] + 1 : 0;
                            }
                        }
                    }
                }
            }
        }



        $tpl = APPPATH.'templates/admin/mform_listc_'.$this->fid.'.html';
        $this->template->assign(array(
            'menu' => $this->get_menu_v3(array(
                fc_lang('返回') => array($this->_get_back_url(APP_DIR.'/home/index'), 'mail-reply'),
                $this->cdata['title'].'：调查结果' => array(APP_DIR.'/admin/'.$this->router->class.'/index/cid/'.$this->cid, $this->form['setting']['icon'] ? str_replace('fa fa-', '', $this->form['setting']['icon']) : 'table'),
            )),
            'value' => $value,
            'wenjuan' => $wenjuan,
        ));
        $this->template->display(is_file($tpl) ? basename($tpl) : 'mform_listc.html');
    }
}